/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 *
 * @author MBG
 */
public class AstronautaProxy implements InvocationHandler {
    
    private Object obj;
    
    private AstronautaProxy(Object obj)
    {
        this.obj = obj;
    }
    
    
    public static Object newInstance(Object obj)
    {
        return java.lang.reflect.Proxy.newProxyInstance(obj.getClass().getClassLoader(),
                obj.getClass().getInterfaces(),
                new AstronautaProxy(obj));
    }
    
    public Object invoke(Object proxy, Method m, Object[] args) throws Throwable
    {
        Object result;
        try
        {
            System.out.println("Antes de llamar al metodo " + m.getName());
            result = m.invoke(obj, args);
        }catch(InvocationTargetException e)
        {
            throw e.getTargetException();
        }catch(Exception ex)
        {
            throw new RuntimeException(ex.getMessage());
        }finally
        {
            System.out.println("Despues de llamar al metodo " + m.getName());
        }
        return result;
    }
}
